from fastapi import APIRouter, Query, Request

from server.app.cloud_ide.service.system_info_service import SystemInfoService
from server.framework.core.http_schemas import CommonResponseSchema
from server.framework.core.logger import logger

system_info_api = APIRouter()
system_info_service = SystemInfoService()


@system_info_api.get("/list_node_info", name='获取节点信息', response_model=CommonResponseSchema)
async def list_node_info(request: Request) -> CommonResponseSchema:
    logger.info(f'查询节点信息,当前用户:[{request.state.user_name}]')
    node_info = system_info_service.get_all_nodes()
    return CommonResponseSchema(data=node_info)


@system_info_api.get("/query_metrics", name='获取指标信息', response_model=CommonResponseSchema)
async def query_metrics(request: Request, node_lists: str = Query(None, description='节点列表'),
                        metric_names: str = Query(None, description='指标名称，多个指标以逗号分割'),
                        start: str = Query(None, description='开始时间'),
                        end: str = Query(None, description='结束时间'),
                        step: str = Query(None, description='聚合步长')
                        ) -> CommonResponseSchema:
    logger.info(
        f'获取指标信息,当前用户:[{request.state.user_name}],节点列表:[{node_lists}],'
        f'指标名称:[{metric_names}],开始时间:[{start}],结束时间:[{end}],聚合步长:[{step}]')

    if node_lists is not None:
        node_lists = node_lists.split(',')
    metrics = metric_names.split(',')
    monitor_value = system_info_service.load_node_metric(node_lists=node_lists, metrics=metrics,
                                                         start=start, end=end, step=step)
    return CommonResponseSchema(success=True, data=monitor_value)
